//
//  NSObject+Hud.m
//  Meili
//
//  Created by Robin on 16/1/6.
//  Copyright © 2016年 Robin. All rights reserved.
//

#import "NSObject+Hud.h"
#import <FontIcon/IconFont.h>

static const NSInteger HudTag = 1314520 + 'H';
static const NSInteger HudTipTag = 1314520 + 'T';
const CGFloat HUD_DELAY_TIME = 1.2;
static const CGFloat TimeOut = 15.;
NSString *const MYErrorDomain = @"MYErrorDomain";
NSString *const MYErrorMsgKey = @"MYErrorMsgKey";
NSString *const MYEmptyDomain = @"MYEmptyDomain";

typedef NS_ENUM(NSUInteger, HUDMsgType) {
    HUDMsgTypeSuccess,
    HUDMsgTypeFailed,
    HUDMsgTypeWarn
};

@implementation NSObject (Hud)
+(void)showProgressInView:(UIView *)view
{
    [self showProgressInView:view withMessage:@"正在加载中..."];
}

+(void)showProgressInView:(UIView *)view withMessage:(NSString *)msg
{
    [self hideHudInView:view];
    MBProgressHUD *hud = [self hudInView:view withText:msg];
    [hud showAnimated:YES];
}

+(MBProgressHUD *)hudInView:(UIView *)view withText:(NSString *)text;
{
    return [self hudInView:view withText:text tag:HudTag];
}

+(MBProgressHUD *)hudInView:(UIView *)view withText:(NSString *)text tag:(NSInteger)tag;
{
    UIView *topView = [UIWindow my_topWindow];
    if(!view || [MYAppHelper shareHelper].isKeyboardVisible){
        view = topView;
    }
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:view];
    if (![view isEqual:topView]) {
        //用于修正在vc里面显示没有垂直居中
        hud.top -= NavBarHeight / 2.;
    }
    hud.bezelView.color = HEXACOLOR(HSColorBlack, 0.7);
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleBlur;
    hud.minShowTime = 1;
    hud.removeFromSuperViewOnHide = YES;
    hud.contentColor = [UIColor whiteColor];
    hud.animationType = MBProgressHUDAnimationZoom;
    //字太长，可能需要换行，用detail
    hud.detailsLabel.font = [UIFont systemFontOfSize:16];
    hud.detailsLabel.text = text;
    [hud hideAnimated:YES afterDelay:TimeOut];
    hud.tag = tag;
    [view addSubview:hud];
    [view bringSubviewToFront:hud];
    
    [hud.button mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(0);
    }];
    
    return hud;
}

+(void)hideHudInView:(UIView *)view
{
    MBProgressHUD *hud = [view viewWithTag:HudTag];
    if(hud){
        [hud hideAnimated:YES];
    }
}

+ (MBProgressHUD *)hudWithText:(NSString *)text tag:(NSInteger)tag
{
    if (tag == HudTipTag) {
        return [self hudInView:[UIWindow my_topWindow] withText:text tag:HudTipTag];
    }
    return [self hudInView:[UIViewController my_topViewController].view withText:text];
}

+(void)showProgress
{
    [self showProgressWithMessage:@"正在加载中..."];
}

+(void)showProgressWithMessage:(NSString *)msg
{
    [self hideProgress];
    MBProgressHUD *hud = [self hudWithText:msg tag:HudTag];
    [hud showAnimated:YES];
}

+(void)showError:(NSError *)error
{
    if (error.code == MYErrorCodeNetworkSlow)
    {
        [self showFailedMsg:@"当前网络缓慢，您再重新试试"];
    }
    else if(error.code == MYErrorCodeCancle)
    {
        if (error.domain == MYErrorDomain) {
            [UIWindow showFailedMsg:error.userInfo[MYErrorMsgKey]];
        }
    }
    else
    {
        if(![MYAppHelper shareHelper].hasNetWork){
            [self showFailedMsg:@"网络不见了，请检查网络连接"];
        }
        else
        {
#if DEBUG
            [self showFailedMsg:error.localizedDescription];
#else
            [self showFailedMsg:@"网络不给力"];
#endif
        }
    }
}

+(void)showWarnMsg:(NSString *)warnMsg
{
    [self showHUDWithMsg:warnMsg type:HUDMsgTypeWarn];
}

+(void)showFailedMsg:(NSString *)failedMsg
{
    [self showHUDWithMsg:failedMsg type:HUDMsgTypeFailed];
}

+(void)showSuccessMsg:(NSString *)successMsg
{
    [self showHUDWithMsg:successMsg type:HUDMsgTypeSuccess];
}

+(void)showHUDWithMsg:(NSString *)msg type:(HUDMsgType)type
{
    [self hideProgress];
    MBProgressHUD *hud = [self hudWithText:msg tag:HudTipTag];
    hud.mode = MBProgressHUDModeCustomView;
    switch (type) {
        case HUDMsgTypeWarn:
            hud.customView = [IconFont labelWithIcon:[IconFont icon:@"alert-circled" fromFont:ionIcons] fontName:ionIcons size:37.0f color:[UIColor whiteColor]];
            break;
        case HUDMsgTypeFailed:
            hud.customView = [IconFont labelWithIcon:[IconFont icon:@"close-circled" fromFont:ionIcons] fontName:ionIcons size:37.0f color:[UIColor whiteColor]];
            break;
        case HUDMsgTypeSuccess:
            hud.customView = [IconFont labelWithIcon:[IconFont icon:@"checkmark-circled" fromFont:ionIcons] fontName:ionIcons size:37.0f color:[UIColor whiteColor]];
            break;
    }
    [hud showAnimated:YES];
    [hud hideAnimated:YES afterDelay:HUD_DELAY_TIME];
}

+(void)hideProgress
{
    UIView *view = [UIViewController my_topViewController].view;
    UIView *win = [UIWindow my_topWindow];
    if(!view){
        view = win;
    }

    [self clearHudInView:view hudTag:HudTag];
    //防止没有清掉
    if (![view isEqual:win]) {
        [self clearHudInView:win hudTag:HudTag];
    }
    
    [self clearHudInView:view hudTag:HudTipTag];
    //防止没有清掉
    if (![view isEqual:win]) {
        [self clearHudInView:win hudTag:HudTipTag];
    }
}

+ (void)clearHudInView:(UIView *)view hudTag:(NSInteger)hudTag
{
    MBProgressHUD *hud = [view viewWithTag:hudTag];
    if(hud){
        [hud hideAnimated:YES];
    }
}

@end
